package com.zlm.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zlm.common.Result;
import com.zlm.entity.AddressBook;
import com.zlm.service.AddressBookService;
import com.zlm.utils.BaseHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址
 */

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public Result<AddressBook> save(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseHolder.getCurrentId());
        log.info("addressBook:{}", addressBook);
        addressBookService.save(addressBook);
        return Result.success(addressBook);
    }


    /**
     * 设置用户默认地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public Result<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        log.info("addressBook:{}", addressBook);
        // 设置一个条件构造器
        // 注意这个updata
        LambdaUpdateWrapper<AddressBook> lambdaQueryWrapper = new LambdaUpdateWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getUserId, BaseHolder.getCurrentId());
        lambdaQueryWrapper.set(AddressBook::getIsDefault, 0);

        // 进行修改 我们先将里面全部变为了0
        addressBookService.update(lambdaQueryWrapper);

        //
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return Result.success(addressBook);
    }


    /**
     * 查询默认地址
     *
     * @return
     */
    @GetMapping("/default")
    public Result<AddressBook> getDefault() {
        // 构造条件器
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getUserId, BaseHolder.getCurrentId());
        lambdaQueryWrapper.eq(AddressBook::getIsDefault, 1);

        AddressBook addressBook = addressBookService.getOne(lambdaQueryWrapper);
        if (addressBook == null) {
            return Result.error("没有设置默认地址");
        }
        return Result.success(addressBook);

    }

    /**
     * 根据id来查询这人所有的收获地址
     *
     * @param addressBook
     * @return
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> list(AddressBook addressBook) {
        // 获取的addressBood 是有没有userId的所以我们要进行设置
        addressBook.setUserId(BaseHolder.getCurrentId());

        // 条件构造器
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 根据userId 来查询
        lambdaQueryWrapper.eq(addressBook.getUserId() != null, AddressBook::getUserId, addressBook.getUserId());
        lambdaQueryWrapper.orderByDesc(AddressBook::getUpdateTime);

        List<AddressBook> list = addressBookService.list(lambdaQueryWrapper);
        return Result.success(list);


    }


    /**
     * 地址数据回显
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<AddressBook> getWithId(@PathVariable String id) {
        AddressBook addressBook = addressBookService.getById(id);
        System.out.println(addressBook.toString());
//        addressBook.setUserId(BaseHolder.getCurrentId());
        return Result.success(addressBook);
    }

    /**
     * 修改地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody AddressBook addressBook) {
        addressBookService.updateById(addressBook);
        return Result.success("修改成功");
    }

    @DeleteMapping
    public Result<String> deleteByIds(String ids) {
        addressBookService.removeById(ids);
        return Result.success("删除成功");
    }


}
